package com.project.livetv.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.google.gson.Gson;
import com.project.livetv.R;
import com.project.livetv.adapter.MyRecycleViewAdapter;
import com.project.livetv.bean.LatestBean;
import com.project.livetv.callback.NetCallBack;
import com.project.livetv.config.NetConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by li on 2016/5/9.
 * 、、最新碎片
 */
public class LatestFragment extends BaseFragment {
    private RecyclerView mRecyclerView;
    private PullRefreshLayout mPullRefreshLayout;
    private MyRecycleViewAdapter myRecycleViewAdapter;
    private GridLayoutManager manager;
    private ProgressDialog mDialog;

    //数据
    private List<LatestBean.LivesBean> mDatas;
    private int position;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.latest_fragment_layout,container,false);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.rcv_latestfragment_item);
        mPullRefreshLayout = (PullRefreshLayout) rootView.findViewById(R.id.prfl_latestFragnebt_refresh);
        iniData();
        setData();
        setListener();
        return rootView;


    }

    private void setListener() {
        mPullRefreshLayout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mDatas.clear();
                myRecycleViewAdapter.notifyDataSetChanged();
                downLoadNewData(1);
                downLoadNewData(1);
                downLoadNewData(1);

                mPullRefreshLayout.setRefreshing(false);
            }
        });



        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.d("tag", "mposition  onScroll   "+position);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                int visibleItemCount = layoutManager.getChildCount();
                int totalItemCount = layoutManager.getItemCount();
                if (visibleItemCount > 0 && newState == RecyclerView.SCROLL_STATE_IDLE && position >= totalItemCount - 1) {

                    downLoadNewData(1);
                    downLoadNewData(1);
                    downLoadNewData(1);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                position = manager.findLastVisibleItemPosition();
                Log.d("tag", "mposition  onScroll   "+position);
            }
        });


    }

    private void setData() {
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(myRecycleViewAdapter);
    }

    private void iniData() {
        mDialog = new ProgressDialog(getContext());
        mDatas = new ArrayList<>();
        downLoadNewData(1);
        downLoadNewData(1);
        downLoadNewData(1);
        manager = new GridLayoutManager(getContext(),3, LinearLayoutManager.VERTICAL,false);
        myRecycleViewAdapter = new MyRecycleViewAdapter(mDatas,getContext());

    }


    //下载数据
    private void downLoadNewData(int port){
        String url = NetConfig.NEW_DATA1+port+NetConfig.NEW_DATA2;
        Log.d("tag","latestUrl==="+url);
        downLoadData(url, new NetCallBack() {
            @Override
            public void onStart() {
                mDialog.show();
            }

            @Override
            public void onSucess(String result) {
                Gson gson = new Gson();
                LatestBean latestBean = gson.fromJson(result, LatestBean.class);
                List<LatestBean.LivesBean> tempData = latestBean.getLives();
                Log.d("tag","tempData=="+tempData.toString());
                mDatas.addAll(tempData);
                mDialog.dismiss();
                myRecycleViewAdapter.notifyDataSetChanged();
                mPullRefreshLayout.setRefreshing(false);

            }

            @Override
            public void onError(String error) {
                mDialog.dismiss();
                Toast.makeText(getContext(), R.string.netError, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinished() {
                mDialog.dismiss();

            }

            @Override
            public void onCancled() {
                mDialog.dismiss();

            }
        });
    }

}
